﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TicketApp.Web.TGIServiceProxy;
using System.Configuration;

namespace TicketApp.Web.Controllers
{
    public class TicketTemplateController : Controller
    {
        //
        // GET: /TicketTemplate/

        public ActionResult Index()
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var templates = context.TicketTemplates;
            return View(templates);
        }

        //
        // GET: /TicketTemplate/Details/5

        public ActionResult Details(int id)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var template = context.TicketTemplates.Where(t => t.Id == id).FirstOrDefault();
            return View(template);
        }

        //
        // GET: /TicketTemplate/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /TicketTemplate/Create

        [HttpPost]
        public ActionResult Create(TicketTemplate ticketTemplate)
        {
            //TODO: not working

            //if (ModelState.IsValid)
            //{
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            context.AddToTicketTemplates(ticketTemplate);
            context.SaveChanges();

            //}
            return RedirectToAction("Index");
        }

        //
        // GET: /TicketTemplate/Edit/5

        public ActionResult Edit(int id)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var ticketTemplate = context.TicketTemplates.Where(t => t.Id == id).FirstOrDefault();
            return View(ticketTemplate);
        }

        //
        // POST: /TicketTemplate/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, TicketTemplate ticketTemplate)
        {
            //TODO:  not working
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            context.AttachTo("TicketTemplates", ticketTemplate);
            context.UpdateObject(ticketTemplate);
            context.SaveChanges();
            return RedirectToAction("Index");

        }

        //
        // GET: /TicketTemplate/Delete/5

        public ActionResult Delete(int id)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var ticketTemplate = context.TicketTemplates.Where(t => t.Id == id).FirstOrDefault();
            return View(ticketTemplate);
        }

        //
        // POST: /TicketTemplate/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, TicketTemplate ticketTemplate)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            context.AttachTo("TicketTemplates", ticketTemplate);
            context.DeleteObject(ticketTemplate);
            context.SaveChanges();
            return RedirectToAction("Index");

        }
    }
}
